<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
  <xsl:output method="html" />
  <!-- global variables -->
  <xsl:variable name="action_noChange" select="'no change'" />
  <xsl:variable name="action_increase" select="'increase'" />
  <xsl:variable name="unit_percentage" select="'percentage'" />
  <xsl:variable name="adjust_doc_filename"
  select="'priceAdjuster.xml'" />
  <!-- root template -->
  <xsl:template match="/">
    <html>
      <head>
        <title>Juicers Part 02-A</title>
      </head>
      <body>
        <xsl:call-template name="GenerateJuicerCostTable">
          <xsl:with-param name="adjustDoc"
          select="document($adjust_doc_filename)" />
        </xsl:call-template>
        <br clear="left" />
        <br clear="left" />
      </body>
    </html>
  </xsl:template>
  <!--
     *************************************************************************
     * Template Name: GenerateJuicerCostTable
     * Description : Generate Jucier Cost table after, applying updates from 
     *               parameter document adjustDoc
     *************************************************************************
-->
  <xsl:template name="GenerateJuicerCostTable">
    <xsl:param name="adjustDoc" />
    <h2>
      <font color="darkblue">Result for 
      <xsl:value-of select="$adjust_doc_filename" /></font>
    </h2>
    <br clear="left" />
    <table border="1" align="left">
      <thead>
        <tr>
          <th bgcolor="black">
            <font color="white">Juicer</font>
          </th>
          <th bgcolor="black">
            <font color="white">Cost</font>
          </th>
        </tr>
      </thead>
      <tbody>
        <xsl:for-each select="juicers/juicer">
          <tr>
            <td>
              <xsl:value-of select="name" />
            </td>
            <td>
              <!-- Output cost, checking for updates to price from external file -->
              <xsl:variable name="adjustAction"
              select="$adjustDoc/priceAdjustment/adjustment/@action" />
              <xsl:choose>
                <xsl:when test="$adjustAction = $action_noChange">
                  <xsl:value-of select="format-number(cost[@currency='USD'], '$##,###0.00')" />
                </xsl:when>
                <xsl:when test="$adjustAction = $action_increase">
                  <xsl:variable name="jCost">
                    <xsl:call-template name="DoIncrease">
                      <xsl:with-param name="unit"
                      select="$adjustDoc/priceAdjustment/adjustment/sizeIncrease/@units" />
                      <xsl:with-param name="base"
                      select="cost[@currency='USD']" />
                      <xsl:with-param name="amt"
                      select="$adjustDoc/priceAdjustment/adjustment/sizeIncrease" />
                    </xsl:call-template>
                  </xsl:variable>
                  <xsl:value-of select="format-number($jCost, '$##,###0.00')" />
                </xsl:when>
                <!-- other action types go here -->
                <xsl:otherwise>
                  <xsl:message terminate="yes">
                    <xsl:text>Invalid value &quot;</xsl:text>
                    <xsl:value-of select="$adjustAction" />
                    <xsl:text>&quot; specified for attribute 'action'.</xsl:text>
                  </xsl:message>
                </xsl:otherwise>
              </xsl:choose>
            </td>
          </tr>
        </xsl:for-each>
      </tbody>
    </table>
  </xsl:template>
  <!--
     *******************************************************
     * Template Name: DoIncrease
     * Description : select increase computation for unit
     *******************************************************
-->
  <xsl:template name="DoIncrease">
    <xsl:param name="unit" />
    <xsl:param name="base" />
    <xsl:param name="amt" />
    <xsl:choose>
      <xsl:when test="$unit = $unit_percentage">
        <xsl:variable name="result">
          <xsl:call-template name="PercentIncrease">
            <xsl:with-param name="base" select="$base" />
            <xsl:with-param name="amt" select="$amt" />
          </xsl:call-template>
        </xsl:variable>
        <xsl:value-of select="$result" />
      </xsl:when>
      <!-- other unit types go here -->
      <xsl:otherwise>
        <xsl:message terminate="yes">
          <xsl:text>Invalid value &quot;</xsl:text>
          <xsl:value-of select="$unit" />
          <xsl:text>&quot; specified for attribute 'units'.</xsl:text>
        </xsl:message>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  <!--
    *********************************************************
    * Template Name: PercentIncrease
    * Description : template for computing percent increase
    *********************************************************
-->
  <xsl:template name="PercentIncrease">
    <xsl:param name="base" />
    <xsl:param name="amt" />
    <xsl:value-of select="$base + ($base * ($amt div 100))" />
  </xsl:template>
</xsl:stylesheet>
